package com.credithc.signature.manager.controller.management;

import cfca.seal.servlet.SealClient;
import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.ro.manager.SealChannelRo;
import com.credithc.signature.common.ro.manager.SealInfoRo;
import com.credithc.signature.common.service.SealInfoService;
import com.credithc.signature.common.vo.manager.SealChannelVo;
import com.credithc.signature.common.vo.manager.SealInfoVo;
import com.credithc.signature.manager.service.SealChannelService;
import com.credithc.signature.manager.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 后端印章
 */

@RestController
@RequestMapping("/manager/scs/v2/")
public class SealChannelController {

    private static final String SUCCESS = "SUCCESS";

    @Value("${signature.cfca.sealUrl}")
    private String host;

    @Autowired
    private SealChannelService sealChannelService;

    @Autowired
    SealInfoService sealInfoService;

    @Autowired
    MessageFactory messageFactory;

    @GetMapping("seal/channel/search")
    public Message search(SealChannelRo sealChannelRo, HttpServletRequest request) {
        PageInfo<SealChannelVo> pageInfo = sealChannelService.search(sealChannelRo);
        Map map = new HashMap<>(16);
        map.put("pageInfo", pageInfo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    @GetMapping("seal/channel/{id}/{status}")
    public Message status(@PathVariable(name = "id", required = true) Integer id, @PathVariable(name = "status", required = true) Integer status) {
        sealChannelService.updateStatus(id, status);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    @GetMapping("seal/channel/sealinfo/{id}")
    public Message getSeal(@PathVariable(name = "id", required = true) Integer id) {
        SealInfoVo sealInfo = sealChannelService.getSealInfo(id);
        if (sealInfo.getCreateTime() != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sealInfo.setFormatCreateTime(format.format(sealInfo.getCreateTime()));
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, sealInfo);
    }

    /**
     * 根据
     *
     * @param id
     * @return
     */
    @GetMapping("seal/channel/sealInfo/{id}")
    public Message status(@PathVariable(name = "id", required = true) Integer id) {
        SealInfoVo sealInfo = sealChannelService.getSealInfo(id);
        Map map = new HashMap<>(16);
        map.put("vo", sealInfo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 更新印章图片
     *
     * @param sealInfoRo
     * @return
     */
    @PostMapping("seal/channel/sealInfo/updateSealImg")
    public Message updateSealImg(@RequestBody SealInfoRo sealInfoRo) throws Exception {
        byte[] image = FileUploadUtil.downloadImg(sealInfoRo.getPictureId());
        sealInfoRo.setImage(image);
        SealClient client = new SealClient(host + SealClient.SLASH + SealClient.MAKE_SEAL_SERVLET);
        String result = sealInfoService.updateSealImg(sealInfoRo,client);
        //返回制章结果
        if (SUCCESS.equals(result)) {
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
        } else {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, result);
        }
    }

}
